package com.efast.dmom.mgmt.controller.les.pda;

import java.util.List;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mgmt.bean.les.pda.PdaQueryTwInvPacktranDetail;
import com.efast.dmom.mgmt.bean.les.pda.PdaReplenishmentConfirmationBean;
import com.efast.dmom.mgmt.entity.les.TwInvPacktranDetail;
import com.efast.dmom.mgmt.entity.master.TmBasDpath;
import com.efast.dmom.mgmt.entity.master.TmBasLoc;
import com.efast.dmom.mgmt.entity.master.TmBasPart;
import com.efast.dmom.mgmt.service.les.TwInvPacktranDetailService;
import com.efast.dmom.mgmt.service.master.TmBasDpathService;
import com.efast.dmom.mgmt.service.master.TmBasLocService;
import com.efast.dmom.mgmt.service.master.TmBasPartService;
import com.efast.dmom.mgmt.utils.Assist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 7.5.4.4 补货确认 (Replenishment confirmation)
 *
 * 补货任务需求 TW_INV_PAL_MRI
 *
 * 补货任务头表 TW_INV_PACKTRAN_HEADER
 *
 * 补货任务明细TW_INV_PACKTRAN_DETAIL
 *
 * 前置条件： 指定源库位-目标库位，系统无需分配库位的补货；现有配置举例-即时补货/窗口补货-按累计消耗
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping(value = "/pdaReplenishmentConfirmation")
@Api(description = "pda-->拉动-->7.5.4.4 补货确认-PDA")
public class PdaReplenishmentConfirmationController {

	private final TwInvPacktranDetailService twInvPacktranDetailService;
	private final TmBasPartService tmBasPartService;
	private final TmBasLocService tmBasLocService;
	private final TmBasDpathService tmBasDpathService;

	@Autowired
	public PdaReplenishmentConfirmationController(TwInvPacktranDetailService twInvPacktranDetailService,
			TmBasPartService tmBasPartService, TmBasLocService tmBasLocService, TmBasDpathService tmBasDpathService) {
		this.twInvPacktranDetailService = twInvPacktranDetailService;
		this.tmBasPartService = tmBasPartService;
		this.tmBasLocService = tmBasLocService;
		this.tmBasDpathService = tmBasDpathService;
	}

	@ApiOperation(value = "【补货完成-确认】", notes = "请求参数：")
	@RequestMapping(value = "/replenishmentFinish", method = RequestMethod.POST)
	public @ResponseBody
	ResultBean replenishmentFinish(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		twInvPacktranDetailService.replenishmentFinish(twInvPacktranDetail);
		return result;
	}

	@ApiOperation(value = "【拣货】", notes = "请求参数：")
	@RequestMapping(value = "/pick", method = RequestMethod.POST)
	public @ResponseBody ResultBean pick(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		// 0:新任务，1：任务确认,2:部分分配，3：分配完成，4：分配失败，5：部分拣货，6拣货完成，7任务完成，8：关闭，9：补货失败
		TwInvPacktranDetail twInvPacktranDetail2 = new TwInvPacktranDetail();
		twInvPacktranDetail2.setTwInvPacktranDetailId(twInvPacktranDetail.getTwInvPacktranDetailId());
		// 实际拣货补货数量
		twInvPacktranDetail2.setPickPacktranQty(twInvPacktranDetail.getActPacktranQty());
		// 实际拣货补货包装数
		twInvPacktranDetail2.setPickPacktranPacknum(twInvPacktranDetail.getActPacktranPacknum());
		// 实际拣货补货包装代码
		twInvPacktranDetail2.setPickPacktranPackno(twInvPacktranDetail.getPickPacktranPackno());
		twInvPacktranDetail2.setTaskStatue("6");
		twInvPacktranDetailService.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail2);
		return result;
	}

	@ApiOperation(value = "【关闭】", notes = "请求参数：{\"twInvPacktranDetailId\":\"\",\"lockUser\":\"\"}")
	@RequestMapping(value = "/close", method = RequestMethod.POST)
	public @ResponseBody ResultBean close(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		if (twInvPacktranDetail.getLockUser() != null) {
			TwInvPacktranDetail detail = twInvPacktranDetailService.selectTwInvPacktranDetailById(
					twInvPacktranDetail.getTwInvPacktranDetailId());
			if (detail.getLockUser().equals(twInvPacktranDetail.getLockUser())) {
				throw new ServiceException("当前记录已经被" + detail.getLockUser() + "锁定任务，其余任何用户均无法操作该任务");
			}
		}
		twInvPacktranDetail.setTaskStatue("8");
		twInvPacktranDetailService.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail);
		return result;
	}

	@ApiOperation(value = "【编辑】", notes = "请求参数： {\"twInvPacktranDetailId\":\"\",\"actPacktranQty\":11,\"lockUser\":\"\"}")
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public @ResponseBody ResultBean edit(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		if (twInvPacktranDetail.getLockUser() != null) {
			TwInvPacktranDetail detail = twInvPacktranDetailService.selectTwInvPacktranDetailById(
					twInvPacktranDetail.getTwInvPacktranDetailId());
			if (detail.getLockUser().equals(twInvPacktranDetail.getLockUser())) {
				throw new ServiceException("当前记录已经被" + detail.getLockUser() + "锁定任务，其余任何用户均无法操作该任务");
			}
		}
		twInvPacktranDetailService.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail);
		return result;
	}

	@ApiOperation(value = "【锁定】", notes = "请求参数：{\"twInvPacktranDetailId\":\"\"")
	@RequestMapping(value = "/lock", method = RequestMethod.POST)
	public @ResponseBody ResultBean lock(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		twInvPacktranDetail.setLockUser(SpringWebUtils.getRequestUser());
		twInvPacktranDetailService.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail);
		return result;
	}

	@ApiOperation(value = "【重置】", notes = "请求参数：{\"twInvPacktranDetailId\":\"\",\"taskStatue\":\"\"}")
	@RequestMapping(value = "/reset", method = RequestMethod.POST)
	public @ResponseBody ResultBean reset(@RequestBody TwInvPacktranDetail twInvPacktranDetail) {
		ResultBean result = new ResultBean(true, "'");
		// 0:新任务，1：任务确认,2:部分分配，3：分配完成，4：分配失败，5：部分拣货，6拣货完成，7任务完成，8：关闭，9：补货失败',
		if ("8".equals(twInvPacktranDetail.getTaskStatue())) {
			twInvPacktranDetail.setTaskStatue("0");
			twInvPacktranDetailService.updateNonEmptyTwInvPacktranDetailById(twInvPacktranDetail);
		} else {
			throw new ServiceException("只有关闭的任务可以重置");
		}
		return result;
	}

	@ApiOperation(value = "【查询补货任务】", notes = "请求参数：")
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public @ResponseBody ResultBean query(@RequestBody PdaReplenishmentConfirmationBean reqDetail) {
		ResultBean result = new ResultBean(true, "'");
		Assist assist = new Assist();
		if (!StringUtil.isNullOrBlank(reqDetail.getPartNoVersion())) {
			assist.setRequires(Assist.andEq("part_no_version", reqDetail.getPartNoVersion()));
		}
		if (!StringUtil.isNullOrBlank(reqDetail.getLockUser())) {
			assist.setRequires(Assist.andEq("lock_user", reqDetail.getLockUser()));
		}
		if (!StringUtil.isNullOrBlank(reqDetail.getAfterPartLoc())) {
			assist.setRequires(Assist.andEq("after_part_loc", reqDetail.getAfterPartLoc()));
		}
		if (!StringUtil.isNullOrBlank(reqDetail.getTaskStatue())) {
			assist.setRequires(Assist.andEq("task_statue", reqDetail.getTaskStatue()));
		}
		if (!StringUtil.isNullOrBlank(reqDetail.getLine())) {
			List<Integer> dpathSeqs = twInvPacktranDetailService.queryTrBasDpathSeq(reqDetail.getLine());
			assist.setRequires(Assist.customRequire("and p_from_loc_id in", ")", dpathSeqs));
		}
		// 是否需要分配库存(0:无来源库位需要分配|1：有来源库位，不需要分配)
		// 页面只显示即时补货/窗口补货-按累计消耗的补货任务：即无需系统分配库位ACALL_FLAG=1的补货任务
		assist.setRequires(Assist.andEq("acall_flag", "1"));
		List<PdaQueryTwInvPacktranDetail> resultList = twInvPacktranDetailService.selectPdaTwInvPacktranDetail(assist);
		if (resultList.size() == 0) {
			throw new ServiceException("没有查询到满足条件的补货记录");
		}
		result.setObject(resultList);
		return result;
	}

	@ApiOperation(value = "【查询所有零件】", notes = "请求参数：无")
	@RequestMapping(value = "/queryPart", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryPart() {
		ResultBean result = new ResultBean(true, "'");
		List<TmBasPart> tmBasParts = tmBasPartService.selectTmBasPart(new Assist());
		result.setObject(tmBasParts);
		return result;
	}

	@ApiOperation(value = "【查询所有库位", notes = "请求参数：无")
	@RequestMapping(value = "/queryLoc", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryLoc() {
		ResultBean result = new ResultBean(true, "'");
		List<TmBasLoc> tmBasParts = tmBasLocService.selectTmBasLoc(new Assist());
		result.setObject(tmBasParts);
		return result;
	}

	@ApiOperation(value = "【查询所有补货路径", notes = "请求参数：无")
	@RequestMapping(value = "/queryDpath", method = RequestMethod.POST)
	public @ResponseBody ResultBean queryDpath() {
		ResultBean result = new ResultBean(true, "'");
		List<TmBasDpath> dpathList = tmBasDpathService.selectTmBasDpath(new Assist(Assist.andEq("path_type", "2")));
		result.setObject(dpathList);
		return result;
	}
}
